package com.suyuan.controller.admin;


import com.github.pagehelper.PageInfo;
import com.suyuan.entity.PigOut;
import com.suyuan.entity.ProInspectInfo;
import com.suyuan.service.ProInspectService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.vo.ProInspectInfoVO;
import com.suyuan.web.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *检验检疫控制器
 */
@Controller
@RequestMapping("/admin/ProInspect")
public class ProInspectController {

    @Resource(name = "proInspectService")
    private ProInspectService proInspectService;
    private Map<String,Object> msg = null;

    /**
     * 查询所有生猪检疫信息
     * 跳转到添加检验检疫信息添加页面
     * @return
     */
    @RequestMapping("toAddPage")
    public String toAddPage(){
        return "proInspect_add";
    }

    /**
     * 添加生猪检验检疫信息
     * @param proInspectInfoVO
     * @return
     */
    @RequestMapping("addProInspect")
    @ResponseBody
    public Object addProInspect(ProInspectInfoVO proInspectInfoVO){
        int code = 0;
        try{
            proInspectService.addProInspect(proInspectInfoVO);
            return JsonMessage.success("添加成功");
        }catch (Exception e){
            return JsonMessage.error("添加失败");
        }
    }



    /**
     * 检验检疫信息
     *      检验生猪编号是否正确
     * @param pid
     * @return
     */
    @RequestMapping(value = "checkPur",method = RequestMethod.GET)
    @ResponseBody
    public Object checkPurcharse(Integer pid){
        msg = new HashMap<String, Object>();
        if(proInspectService.checkPurcharse(pid) == false){
            return JsonMessage.error("生猪信息不存在");
        }else{
            return JsonMessage.success("生猪信息存在");
        }
    }


    /**
     * 根据生猪ID查询检验信息
     * @param purGid
     * @return
     */
    @RequestMapping("findByCondition")
    public String list(Integer purGid, Page page, ModelMap modelMap){
        PageInfo pageInfo = proInspectService.findByPurGid(purGid,page);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("page",page);
        modelMap.addAttribute("purGid",purGid);
        return  "proInspect_list";
    }


    /**
     * 生猪检验检疫信息删除
     * @param
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Object deletePigOutInfo(String ids){
        Integer code = proInspectService.deletePigProInspectInInfo(ids);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }


    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Integer deletePigOut(Integer id){
        int code = proInspectService.deleteInfo(id);
        return code;
    }

    /**
     * 跳转编辑页面
     * @param gid
     * @return
     */
    @RequestMapping("toedit")
    public String toeditPage(Integer gid ,ModelMap modelMap){
        ProInspectInfo proInspectInfo = proInspectService.findByGid(gid);
        modelMap.addAttribute("proInspectInfo",proInspectInfo);
        return "proInspect_edit";
    }


    /**
     * 更新生猪检验检疫信息
     * @param proInspectInfo
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Integer updateInfo(ProInspectInfo proInspectInfo){
        int code = 0;
        try{
            proInspectService.updateInfo(proInspectInfo);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }










}
